/*
 * File Name：CameraPreview.java
 * Copyright：Copyright 2008-2013 CiWong.Inc. All Rights Reserved.
 * Description： CameraPreview.java
 * Modify By：PLA-ZJLIU
 * Modify Date：2013-3-27
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.ciwong.libs.utils.CWLog;

/**
 * 预览拍照图
 * 
 * 
 * @author PLA-ZJLIU
 * @version ciwong v.1.0 2013-3-27
 * @since ciwong v.1.0
 */
public class CameraPreview extends SurfaceView implements
        SurfaceHolder.Callback
{
    private static final String TAG = "CameraPreview";

    private boolean isCreated;

    private SurfaceHolder mHolder;

    private Callback mCallback;

    private boolean canPreview;

    /**
     * 预览拍照图
     * 
     * @param context
     *            上下文
     * @param attrs
     *            属性设置
     * @param defStyle
     *            样式
     */
    public CameraPreview(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * 
     * 预览拍照图
     * 
     * @param context
     *            上下文
     * @param attrs
     *            属性设置
     */
    public CameraPreview(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    /**
     * 预览拍照图
     * 
     * @param context
     *            上下文
     */
    public CameraPreview(Context context)
    {
        super(context);
        init();
    }

    void init()
    {
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    /**
     * 
     * 设置是否可预览
     * 
     * @param preview
     *            是否可预览
     */
    public void enablePreview(boolean preview)
    {
        canPreview = preview;
    }

    /**
     * 重新初始化
     * 
     */
    public void reInit()
    {
        if (mHolder != null && isCreated)
        {
            CWCamera.instance().initialPreview(mHolder);
        }
    }

    /**
     * 创建 {@inheritDoc}
     */
    public void surfaceCreated(SurfaceHolder holder)
    {
        CWLog.e(TAG, "surfaceCreated");
        if (mCallback != null)
        {
            mCallback.created();
        }
        CWCamera.instance().initialPreview(holder);
        Camera camera = CWCamera.instance().getCamera();
//        if (camera != null)
//        {
//            camera.autoFocus(new AutoFocusCallback()
//            {
//
//                @Override
//                public void onAutoFocus(boolean success, Camera camera)
//                {
//
//                }
//            });
//        }

        isCreated = true;
        // CWCamera.instance().startPreview();
        Log.i("debug", "surfaceCreated");
    }

    /**
     * 销毁 {@inheritDoc}
     */
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        if (mCallback != null)
        {
            mCallback.destroyed();
        }
        CWCamera.instance().release();
        Log.i("debug", "surfaceDestroyed");
        isCreated = false;
    }

    /**
     * 改变 {@inheritDoc}
     */
    public void surfaceChanged(SurfaceHolder holder, int format, final int w,
            final int h)
    {
        if (w <= 0 || h <= 0 || !canPreview)
        {
            return;
        }
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    CWLog.e(TAG, "surfaceChanged");
                    CWCamera.instance().startPreview(w, h, getContext());
                    CWCamera.instance().setPreRatio(w, h);
                    if (mCallback != null)
                    {
                        mCallback.openCamera();
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 
     * 预览监听
     * 
     * @param callback
     *            预览回调接口
     */
    public void setCreatedListener(Callback callback)
    {
        this.mCallback = callback;
    }

    /**
     * 预览回调接口
     * 
     * @author PLA-ZJLIU
     * 
     */
    public static interface Callback
    {
        /**
         * 创建回调接口
         */
        void created();

        /**
         * 销毁回调接口
         */
        void destroyed();

        void openCamera();
    }
}
